package d_threadpool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*
    线程池的使用
 */
public class MyThreadPoolDemo01 {
    public static void main(String[] args) {
        //创建一个固定容量的线程池
        ExecutorService pool = Executors.newFixedThreadPool(3);
        //创建任务类
        Runnable run = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    Thread t = Thread.currentThread();
                    System.out.println(t.getName() + ":" + i);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        //将任务交给线程池管理
        pool.submit(run);
        pool.submit(run);
        pool.submit(run);
        pool.submit(run);

        pool.shutdown();
    }
}
